from django.db import models
from django_extensions.db.fields import AutoSlugField
from django.urls import reverse

class Tag(models.Model):
    name = models.CharField(
        max_length=31,
        unique=True,
    )
    slug = AutoSlugField(
        help_text="A label for URL config.",
        max_length=31,
        populate_from=["name"],
    )

    class Meta:
        ordering = ["name"]

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        """Return URL to detail page of Tag"""
        return reverse(
            "tag_detail", kwargs={"slug": self.slug}
        )

    def get_update_url(self):
        """Return URL to update page of Tag"""
        return reverse(
            "tag_update", kwargs={"slug": self.slug}
        )

    def get_delete_url(self):
        """Return URL to delete page of Tag"""
        return reverse(
            "tag_delete", kwargs={"slug": self.slug}
        )

class Startup(models.Model):
    name = models.CharField(
        max_length=31,
        db_index=True,
    )
    slug = models.SlugField(
        max_length=31,
        unique=True,
        help_text="A label for URL config."
    )
    description = models.TextField()
    founded_date = models.DateField("date founded")
    contact = models.EmailField()
    website = models.URLField(max_length=255)
    tags = models.ManyToManyField(Tag)

    class Meta:
        get_latest_by = "founded_date"
        ordering = ["name"]

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        """Return URL to detail page of Startup"""
        return reverse(
            "startup_detail", kwargs={"slug": self.slug}
        )

    def get_update_url(self):
        """Return URL to update page of Startup"""
        return reverse(
            "startup_update", kwargs={"slug": self.slug}
        )

    def get_delete_url(self):
        """Return URL to delete page of Startup"""
        return reverse(
            "startup_delete", kwargs={"slug": self.slug}
        )

    def get_newslink_create_url(self):
        """Return URL to delete page of Startup"""
        return reverse(
            "startup_create",
            kwargs={"slug": self.slug}
        )

class NewsLink(models.Model):
    title = models.CharField(max_length=31)
    slug = models.SlugField(max_length=31)
    pub_date = models.DateField("date published")
    link = models.URLField(max_length=255)
    startup = models.ForeignKey(Startup, on_delete=models.CASCADE)

    class Meta:
        get_latest_by = "pub_date"
        ordering = ["pub_date"]
        unique_together = ("slug", "startup")

    def __str__(self):
        return f"{self.startup}:{self.title}"

    def get_absolute_url(self):
        """Return URL to detail page of Startup"""
        return reverse(
            "startup_detail",
            kwargs={"slug": self.startup.slug},
        )

    def get_update_url(self):
        """Return URL to update page of Startup"""
        return reverse(
            "newslink_update",
            kwargs={
                "startup_slug": self.startup.slug,
                "newslink_slug": self.slug,
            },
        )

    def get_delete_url(self):
        """Return URL to delete page of Startup"""
        return reverse(
            "newslink_delete",
            kwargs={
                "startup_slug": self.startup.slug,
                "newslink_slug": self.slug,
            },
        )